<template>
	<view class="container">
		<view class="main">		
			<view>				
				重新设置支付限额（需要指纹认证）
			</view>				
			<view>
				<input type="number" placeholder="设置每笔支付限额" class="pay-limit" v-model="payLimit">
			</view>
		</view>
		<view>
			<button type="primary" @click="changePayLimit">立即修改支付限额</button>
		</view>
	</view>
</template>

<script>
	import Global from '../../common/global.js'
	
	export default {
		data() {
			return {
				customerNumber:'',				
				payLimit:''
			}
		},
		methods: {
			//修改支付限额
			changePayLimit(){
				
				if (this.payLimit <= 0) {
					uni.showToast({
						title:'支付限额输入不正确',
						icon:'none'
					})
					
					return
				}
								
				//验证是否已开启指纹验证
				let enableFingerPrint = uni.getStorageSync('enableFingerPrint')
				
				if (enableFingerPrint == '' || enableFingerPrint == null || enableFingerPrint == 'false') {
					uni.showToast({
						title:'修改支付限额需要开启指纹验证',
						icon:'none'
					})
					
					return
				}
				
				//指纹验证
				uni.startSoterAuthentication({
				    requestAuthModes: ['fingerPrint'],
				    challenge: '123456',
				    authContent: '请使用指纹验证',
				    success:(res)=> {
				        if (res.errCode == 0) {
							
							//调用修改密码接口
							uni.request({
								url: Global.BASE_URL + '/ewallet/updatePayLimit',
								header:{
									'content-type': 'application/x-www-form-urlencoded'
								},
								data:{
									customerNumber:this.customerNumber,
									payLimit:this.payLimit
								},
								method:'POST',
								success:(res)=>{ //注意使用箭头函数						
									if (res.statusCode == 200) {
										uni.showToast({
											title:'支付限额修改成功'
										})
										
										//重新存储支付限额 
										uni.setStorageSync('payLimit',this.payLimit)
										
										setTimeout(()=>{
											//跳转到主画面
											uni.switchTab({
												url:'../home/home'
											})
										},500)
									} else {
										uni.showToast({
											title:'密码修改失败',
											icon:'none'
										})
									}
								},
								fail(err) {
									uni.showToast({
										title:'支付密码接口调用失败',
										icon:'none'
									})
								}
							})
							
						}												
				    },
				    fail:(err)=>{
				        console.log(err);
						
						if (err.errCode == 90010) {
							uni.showToast({
								title:'验证次数过多，指纹登录被暂时冻结，请稍后再试',
								icon:'none'
							})
						}
				    }
				})
				
				
			}			
		},
		onLoad() {
			//读取出客户编号
			this.customerNumber = uni.getStorageSync('customerNumber')			
		},
		onShow() {
			//读取支付限额
			this.payLimit = uni.getStorageSync("payLimit")
		}
	}
</script>

<style>
	.container {		
		padding: 15rpx;
	}
	
	.main {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
		
			
	.pay-limit {
		width: 600rpx;
		height: 150rpx;
		color: #333333;
		font-size: 48rpx;
		border-bottom: 1px solid #C0C0C0;
		margin-bottom: 50rpx;
	}
</style>